<?php

namespace App\Http\Controllers\Admin;

use App\Repository\GetWay\WayExamRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\UploadRepository;

// 分销商
class WayExamController extends BaseController
{
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function store(Request $request)
  {
    // var_dump($request->get('start_time'));
    // var_dump($request->get('start_time')[0]);
    // var_dump($request->get('start_time')[1]);
    if (!empty($request->get('start_time'))) {
      if (is_array($request->get('start_time'))) {
        $request->offsetSet('end_time', $request->get('start_time')[1]);
        $request->offsetSet('start_time', $request->get('start_time')[0]);
      }
    }
    // var_dump($request->get('start_time'));
    // var_dump($request->get('end_time'));
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if ($request->user->role_id != 1) {
      $where['where']['shop_id'] = $request->user->shop_id;
    }
    if (!empty($request->get('phone'))) {
      $where['where']['phone'] = $request->get('phone');
    }
    if (!empty($request->get('name'))) {
      $where['like'] = ['name' => $request->get('name')];
    }
    if (!empty($request->get('number'))) {
      $where['like'] = ['name' => $request->get('number')];
    }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    $data =  $this->repoist->index($where);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    if (!empty($request->get('start_time'))) {
      if (is_array($request->get('start_time'))) {
        $request->offsetSet('end_time', $request->get('start_time')[1]);
        $request->offsetSet('start_time', $request->get('start_time')[0]);
      }
    }
    $where = ['id' => $request->get('id')];
    return $this->repoist->edit($where, $request);
  }
  public function  show($id)
  {
    $where = ['id' => $id];
    //查询一条数据
    $return = $this->repoist->getOne($where, ['*']);
    if (!empty($return)) {
      // $return = usfirstArr($return);
      // 将文本模式 格式化
      // if (!empty($return->goods_detail)) {
      //   $return->goods_detail = html_entity_decode($return->goods_detail);
      // }
      // if (!empty($return->goods_remark)) {
      //   $return->goods_remark = html_entity_decode($return->goods_remark);
      // }
      if (!empty($return->start_time)) {
        $return->start_time = [
          date('Y-m-d') . ' ' . $return->start_time,
          date('Y-m-d') . ' ' . $return->end_time,
        ];
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    // return $this->returnMsg(201, '不能删除数据');
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  // 导入功能
  public function import(Request $request)
  {
    $model = new UploadRepository();
    $res = $model->upload($request, 'file');
    if (empty($res['file_src'])) {
      return [
        'code' => 100,
        'msg' => '上传失败',
        'fileName' => ''
      ];
    }
    $request['file_name'] = $res['file_name'];
    $request['upload_rout'] = $res['file_src'];

    return $this->repoist->add($request);
  }
}
